package com.ali.ding;

import android.content.Context;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.Nullable;

import com.ding.rtc.DingRtcEngine;
import com.ding.rtc.DingRtcErrorCode;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.platform.PlatformView;

class DingRtcView implements PlatformView {

//    private MethodChannel mChannel;
    private int mViewId;
    DingRtcEngine.DingRtcVideoCanvas mCanvas;
    private SurfaceView mView;

    DingRtcView(Context context, BinaryMessenger messenger, int viewId) {
        super();
        this.mViewId = viewId;
//        mChannel = new MethodChannel(messenger,  "DingRtcView_" + viewId);
//        mChannel.setMethodCallHandler(this);

        mCanvas = new DingRtcEngine.DingRtcVideoCanvas();
        mCanvas.renderMode = DingRtcEngine.DingRtcRenderMode.DingRtcRenderModeAuto;
        mView = DingRtcManager.getInstance().createRenderSurfaceView(context);
        if (mView == null) {
            DingRtcManager.getInstance().onError(DingRtcErrorCode.ERR_INNER, "DingRtcView Create failure");
        } else {
            mView.setZOrderMediaOverlay(true);
            mCanvas.view = mView;
        }
        DingRtcManager.getInstance().addView(viewId, this);
    }

    @Nullable
    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void dispose() {
        DingRtcManager.getInstance().removeView(mViewId);
        mView = null;
        mCanvas = null;
//        mChannel.setMethodCallHandler(null);
    }
}
